package com.infore.airsupervise.utils;

import android.content.Context;
import android.content.DialogInterface;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.infore.airsupervise.R;
import com.infore.airsupervise.app.MsgConst;
import com.rey.material.app.DatePickerDialog;
import com.rey.material.app.Dialog;
import com.rey.material.app.DialogFragment;
import com.rey.material.app.SimpleDialog;
import com.rey.material.app.TimePickerDialog;

import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * 对话框工具类
 * 对materialDialog的封装
 */

public class DialogUtil implements SweetAlertDialog.OnDismissListener{

    private static DialogUtil instance;
    private SweetAlertDialog sDialog;
    private DialogUtil() {}

    public static DialogUtil getInstance() {
        if (instance == null) {
            return new DialogUtil();
        }
        return instance;
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        // 防止内存泄露
        if (sDialog != null) {
            sDialog = null;
        }

    }

    /**
     * Dialog动作监听
     */
    public interface OnDialogActionListener {
        /** 确定 */
        void onPositiveActionClicked(DialogFragment fragment);
        /** 中性 */
//        void onNeutralActionClicked(DialogFragment fragment);
        /** 否定 */
        void onNegativeActionClicked(DialogFragment fragment);
    }

    /**
     * 私有的构建方法
     * @param listener
     * @return
     */
    private Dialog.Builder commonBuild(final OnDialogActionListener listener) {
        Dialog.Builder builder = new SimpleDialog.Builder(R.style.SimpleDialogLight) {
            @Override
            public void onPositiveActionClicked(DialogFragment fragment) {
                if (null != listener) {
                    listener.onPositiveActionClicked(fragment);
                }
                super.onPositiveActionClicked(fragment);
            }

            @Override
            public void onNegativeActionClicked(DialogFragment fragment) {
                if (null != listener) {
                    listener.onNegativeActionClicked(fragment);
                }
                super.onNegativeActionClicked(fragment);
            }
        };
        return builder;
    }

    /**
     * 基本Dialog
     * @param title
     * @param content
     * @param positive
     * @param negative
     * @param listener 监听
     * @return
     */
    public DialogFragment basic(
            String title, String content, String positive, String negative, OnDialogActionListener listener) {
        Dialog.Builder builder = commonBuild(listener);

        if (null != content)
            ((SimpleDialog.Builder)builder).message(content);

        if (null != title)
            builder.title(title);

        builder.positiveAction(null == positive ? MsgConst.CONFIRM : positive);
        builder.negativeAction(null == negative ? MsgConst.CANCLE : negative);

        return DialogFragment.newInstance(builder);
    }

    public DialogFragment basic(String title, String content, OnDialogActionListener listener) {
        return basic(title, content, null, null, listener);
    }

    public DialogFragment basic(String title, String content) {
        return basic(title, content, null);
    }

    /**
     * 单选
     *
     * SimpleDialog dialog = (SimpleDialog) fragment.getDialog();
     * dialog.getSelectedValue()
     *
     * @param title
     * @param items
     * @param selected
     * @param listener
     * @return
     */
    public DialogFragment radioChoice(String title, String[] items, int selected, OnDialogActionListener listener) {
        Dialog.Builder builder = commonBuild(listener);

        ((SimpleDialog.Builder)builder).items(items, selected)
                .positiveAction(MsgConst.CONFIRM)
                .negativeAction(MsgConst.CANCLE);

        if (null != title)
            builder.title(title);

        return DialogFragment.newInstance(builder);
    }

    /**
     * 多选
     *
     * SimpleDialog dialog = (SimpleDialog) fragment.getDialog();
     * dialog.getSelectedValues()
     *
     * @param title
     * @param items
     * @param selected
     * @param listener
     * @return
     */
    public DialogFragment multiChoice(String title, String[] items, int[] selected, OnDialogActionListener listener) {
        Dialog.Builder builder = commonBuild(listener);

        ((SimpleDialog.Builder)builder).multiChoiceItems(items, selected)
                .positiveAction(MsgConst.CONFIRM)
                .negativeAction(MsgConst.CANCLE);

        if (null != title)
            builder.title(title);

        return DialogFragment.newInstance(builder);
    }

    /**
     * 时间选择
     *
     * TimePickerDialog dialog = (TimePickerDialog) fragment.getDialog();
     * String timeStr = dialog.getFormattedTime(new SimpleDateFormat(DateUtil.FORMAT_TIME));
     *
     * @param listener
     * @return
     */
    public DialogFragment timePicker(final OnDialogActionListener listener) {
        TimePickerDialog.Builder builder = new TimePickerDialog.Builder(R.style.Material_App_Dialog_TimePicker_Light, 24, 00) {
            @Override
            public void onPositiveActionClicked(DialogFragment fragment) {
                if (null != listener) {
                    listener.onPositiveActionClicked(fragment);
                }
                super.onPositiveActionClicked(fragment);
            }

            @Override
            public void onNegativeActionClicked(DialogFragment fragment) {
                if (null != listener) {
                    listener.onNegativeActionClicked(fragment);
                }
                super.onNegativeActionClicked(fragment);
            }
        };

        builder.positiveAction(MsgConst.CONFIRM).negativeAction(MsgConst.CANCLE);
        return DialogFragment.newInstance(builder);
    }

    /**
     * 日期选择
     *
     * DatePickerDialog dialog = (DatePickerDialog) fragment.getDialog();
     * String dateStr = dialog.getFormattedDate(new SimpleDateFormat(DateUtil.FORMAT_DATE)));
     *
     * @param listener
     * @return
     */
    public DialogFragment datePicker(final OnDialogActionListener listener, long min , long max) {
        DatePickerDialog.Builder builder = new DatePickerDialog.Builder(R.style.Material_App_Dialog_DatePicker_Light){
            @Override
            public void onPositiveActionClicked(DialogFragment fragment) {
                if (null != listener) {
                    listener.onPositiveActionClicked(fragment);
                }
                super.onPositiveActionClicked(fragment);
            }
            @Override
            public void onNegativeActionClicked(DialogFragment fragment) {
                if (null != listener) {
                    listener.onNegativeActionClicked(fragment);
                }
                super.onNegativeActionClicked(fragment);
            }
        };
        builder.positiveAction(MsgConst.CONFIRM).negativeAction(MsgConst.CANCLE);
        if( max !=0) {
            builder.dateRange(min, max);
            builder.date(max);
        }
        return DialogFragment.newInstance(builder);
    }

    public void dialog(Context ctx){
        Dialog dialog1=null;
        //得到布局
        View view= LayoutInflater.from(ctx).inflate(R.layout.dialog_loading, null);
        LinearLayout layout = (LinearLayout) view.findViewById(R.id.dialog_view);
        //创建自定义dialog
        dialog1= new Dialog(ctx, R.style.loading_dialog);
        dialog1.addContentView(layout, new ViewGroup.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        dialog1.setCancelable(true);
        dialog1.show();
    }

    public SweetAlertDialog alertProgress(Context ctx, String title, boolean cancelable) {
        sDialog = new SweetAlertDialog(ctx, SweetAlertDialog.PROGRESS_TYPE);
        sDialog.getProgressHelper().setBarColor(ctx.getResources().getColor(R.color.colorPrimary));
        sDialog.setTitleText(title)
                .setCancelable(cancelable);
        sDialog.show();
        sDialog.setOnDismissListener(this);
        return sDialog;
    }
}
